package com.taptech.doufu.weex.module;

import android.app.Activity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.taobao.weex.common.WXModule;
import com.taobao.weex.common.WXModuleAnno;
import com.taptech.doufu.ui.activity.weex.TFWXActivity;
import com.taptech.doufu.ui.activity.base.DiaobaoBaseActivity;
import com.taptech.doufu.services.personalcenter.AccountService;
import com.taptech.doufu.weex.QLXApplicationUtil;

import java.util.HashMap;


/**
 * Created by qlx on 2016/12/8.
 */

public class TFEventModule extends WXModule {

    @WXModuleAnno(moduleMethod = true,runOnUIThread = true)
    public void openURL(String url,String data) {
        try {
            if (url instanceof String){

                if (url.indexOf("needLogin") > 0){
                    if (AccountService.getInstance().isLogin() == false) {
                        AccountService.getInstance().jumpToLogin();
                        return ;
                    }
                }

                HashMap<String, Object> param = new HashMap<>();
                param.put("url",url);

                JSONObject dataParam = JSON.parseObject(data);
                if (url.indexOf("present") > 0){
                        if (data != null){
                            param.putAll(dataParam);
                        }
                        if (mWXSDKInstance.getContext() instanceof TFWXActivity) {
                            TFWXActivity ac = (TFWXActivity) mWXSDKInstance.getContext();
                            ac.presentWeexView(url , param);
                        }else if(mWXSDKInstance.getContext() instanceof DiaobaoBaseActivity){
                            DiaobaoBaseActivity activity = (DiaobaoBaseActivity)mWXSDKInstance.getContext();
                            activity.presentWeexView(url , param);
                        }
                        return;
                    }else{
                        if (data != null){
                            param.put("data",dataParam);
                        }
                        QLXApplicationUtil.pushActivity((Activity) mWXSDKInstance.getContext(), TFWXActivity.class,param);
                    }


            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
